<?php

namespace App\Http\Controllers\Api;

use App\Models\Article;
use App\Http\Controllers\Controller;
use App\Models\view;
use Illuminate\Http\Request;

class NewsController extends Controller
{
    public function getlist(Request $request)
    {
        $page   = $request->get('page');
        $select = [
            'id',
            'title',
            'pic',
            'url',
            'desn',
            'created_at'
        ];
        $data   = Article::orderBy('id', 'desc')->select($select)->paginate(5, ['*'], 'page', $page);
        return $data;
    }

    // 获取当前文章信息
    public function show(Article $article)
    {
        return $article;
    }

    // 文章阅读人数统计
    public function count(Request $request, int $article)
    {
        $openid = $request->get('openid');
        $data   = [
            'openid'    => $openid,
            'article_id'    => $article,
            'view_date' => date('Y-m-d'),
            'view_time' => time()
        ];
        try {
            $model = view::create($data);
        } catch (\Exception $exception) {
            return ['status' => 1007, 'msg' => '数据已存在'];
        }
        return $model;
    }
}
